/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.twistinjava.action;

import com.twistinjava.adminDAO.AdminDAO;
import com.twistinjava.category.model.Category;
import com.twistinjava.category.modelDAO.CategoryDAO;
import com.twistinjava.logon.User;
import com.twistinjava.userDAO.UserDAO;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.ActionDispatcher;

/**
 *
 * @author shikhar
 */
public class AdminAction extends BaseAction
{

    private static Log log = LogFactory.getLog(AdminAction.class);
    private String SUCCESS = "";
    protected ActionDispatcher dispatcher = new ActionDispatcher(this, ActionDispatcher.DEFAULT_FLAVOR);

    @Override
    public ActionForward twistinjavaExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        return dispatcher.execute(mapping, form, request, response);
    }

    public ActionForward welcomeHome(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside welcomeHome method of AdminAction");

        SUCCESS = "home";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward createUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside welcomeHome method of AdminAction");

        SUCCESS = "createUser";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward saveUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside saveUser method of AdminAction");
        ActionMessages actionMessages = new ActionMessages();
        try
        {
            User user = new User();
            user.setFirstName(request.getParameter("fName") == null ? "" : request.getParameter("fName"));
            user.setLastName(request.getParameter("lName") == null ? "" : request.getParameter("lName"));
            user.setEmail(request.getParameter("email") == null ? "" : request.getParameter("email"));
            user.setLogonID(request.getParameter("loginID") == null ? "" : request.getParameter("loginID"));
            user.setPassword(request.getParameter("password") == null ? "" : request.getParameter("password"));
            AdminDAO adminDAO = new AdminDAO();
            adminDAO.saveuser(user);
            String name = user.getFirstName() + " " + user.getLastName();
            log.debug(name + " added successfully");
            actionMessages.add("message", new ActionMessage("user.added.successfully", name + " added successfully."));
            saveMessages(request, actionMessages);
        }
        catch (Exception exception)
        {
            log.error("Exception inside saveuser method of AdminAction class :" + exception.getMessage());
            actionMessages.add("message", new ActionMessage("admin.error.message", exception.getMessage()));
            saveMessages(request, actionMessages);
        }
        SUCCESS = "createUser";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward modifyUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside modifyUser method of AdminAction");
        ActionMessages actionMessages = new ActionMessages();
        try
        {
            ArrayList allUser = new ArrayList();
            UserDAO userDAO = new UserDAO();
            allUser = userDAO.getAllUser();
            request.setAttribute("allUser", allUser);
        }
        catch (Exception exception)
        {
            log.error("Exception inside modifyUser method of AdminAction class :" + exception.getMessage());
            actionMessages.add("message", new ActionMessage("admin.error.message", exception.getMessage()));
            saveMessages(request, actionMessages);
        }
        SUCCESS = "modifyUser";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside deleteUser method of AdminAction");
        ActionMessages actionMessages = new ActionMessages();
        UserDAO userDAO = new UserDAO();
        try
        {
            int id = Integer.parseInt(request.getParameter("id"));
            User user = userDAO.getUser(id);
            String name = user.getFirstName() + " " + user.getLastName();
            userDAO.deleteUser(id);
            actionMessages.add("message", new ActionMessage("user.deleted.successfully", name + " deleted successfully."));
            saveMessages(request, actionMessages);
            ArrayList allUser = new ArrayList();


            allUser = userDAO.getAllUser();
            request.setAttribute("allUser", allUser);
        }
        catch (Exception exception)
        {
            log.error("Exception inside deleteUser method of AdminAction class :" + exception.getMessage());
            actionMessages.add("message", new ActionMessage("admin.error.message", exception.getMessage()));
            saveMessages(request, actionMessages);
        }
        SUCCESS = "modifyUser";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward createCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside createCategory method of AdminAction");

        SUCCESS = "createCategory";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward saveCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside saveCategory method of AdminAction");
        ActionMessages actionMessages = new ActionMessages();
        try
        {
            Category category = new Category();

            category.setCategoryName(request.getParameter("cName") == null ? "" : request.getParameter("cName"));
            CategoryDAO categoryDAO = new CategoryDAO();

            categoryDAO.saveCategory(category);
            String cName = category.getCategoryName();
            log.debug(cName + " added successfully");
            actionMessages.add("message", new ActionMessage("category.added.successfully", cName + " added successfully."));
            saveMessages(request, actionMessages);
        }
        catch (Exception exception)
        {
            log.error("Exception inside saveCategory method of AdminAction class :" + exception.getMessage());
            actionMessages.add("message", new ActionMessage("admin.error.message", exception.getMessage()));
            saveMessages(request, actionMessages);
        }
        SUCCESS = "createCategory";
        return mapping.findForward(SUCCESS);
    }
    public ActionForward modifyCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside modifyCategory method of AdminAction");
        ActionMessages actionMessages = new ActionMessages();
        try
        {
            ArrayList allCategory = new ArrayList();
            CategoryDAO categoryDAO = new CategoryDAO();
            allCategory = categoryDAO.getAllCategory();
            request.setAttribute("allCategory", allCategory);
        }
        catch (Exception exception)
        {
            log.error("Exception inside modifyCategory method of AdminAction class :" + exception.getMessage());
            actionMessages.add("message", new ActionMessage("admin.error.message", exception.getMessage()));
            saveMessages(request, actionMessages);
        }
        SUCCESS = "modifyCategory";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward deleteCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside deleteCategory method of AdminAction");
        ActionMessages actionMessages = new ActionMessages();
        CategoryDAO categoryDAO = new CategoryDAO();
        try
        {
            int id = Integer.parseInt(request.getParameter("id"));
            Category category = categoryDAO.getCategory(id);
            String catName = category.getCategoryName();
            categoryDAO.deleteCategory(id);
            actionMessages.add("message", new ActionMessage("category.deleted.successfully", catName + " deleted successfully."));
            saveMessages(request, actionMessages);
            ArrayList allCategory = new ArrayList();


            allCategory = categoryDAO.getAllCategory();
            request.setAttribute("allCategory", allCategory);
        }
        catch (Exception exception)
        {
            log.error("Exception inside deleteCategory method of AdminAction class :" + exception.getMessage());
            actionMessages.add("message", new ActionMessage("admin.error.message", exception.getMessage()));
            saveMessages(request, actionMessages);
        }
        SUCCESS = "modifyCategory";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward createSubCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        log.debug("Inside createSubCategory method of AdminAction");
        CategoryDAO categoryDAO = new CategoryDAO();
        ArrayList allCategory = categoryDAO.getAllCategory();
        request.setAttribute("allCategory", allCategory);

        SUCCESS = "createSubCategory";
        return mapping.findForward(SUCCESS);
    }

}
